﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
<table border="0" width="100%">
    <tr>
        <td valign="middle" align="left" width="400px">
            <span style="font-size: 14px">担保公司:</span><select id="ddlguaranteeCompany" name="ddlguaranteeCompany"
                class="selectpicker" data-style="btn-info" data-width="300px" style="margin-top: 10px">
            </select>
        </td>
        <td valign="middle" align="left" width="350px">
            <span style="font-size: 14px">关键字:</span>
            <input id="txtKey" name="txtKey" type="text" class="input-medium search-query" placeholder="编码/名称等"
                style="width: 250px; background-color: #fff;" />
        </td>
        <td valign="middle" align="left">
            <button class="btn btn-primary btn-small" id="btnSearch">
                查询
            </button>
        </td>
    </tr>
</table>
<div style="width:99%;height:1px;overflow:hidden;background-color:#dddddd;margin:10px"></div>
<table id="projectgrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>
<div id="memberform" class="modal fade" data-backdrop="static" tabindex="-1" style="display: none;
    width: 700px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 class="modal-title" style="font-size: 17px; font-weight: bold" id="modaltitle">
            借款人开户</h3>
    </div>
    <table border="0" cellpadding="0" cellspacing="0" style="margin-top: 10px;">
        <tr>
            <td valign="middle" width="100px" align="right">
                用户名:
            </td>
            <td>
                <input type="text" id="txtUserName" placeholder="用户名" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
            <td valign="middle" align="right" width="100px">
                真实姓名:
            </td>
            <td>
                <input type="text" id="txtRealName" placeholder="真实姓名" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                身份证号:
            </td>
            <td>
                <input type="text" id="txtAgentIDNO" placeholder="身份证号" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
            <td valign="middle" align="right">
                手机号码:
            </td>
            <td>
                <input type="text" id="txtAgentHandPhone" placeholder="手机号码" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                居住地址:
            </td>
            <td colspan="3">
                <input type="text" id="txtAddress" placeholder="居住地址" style="width: 535px" class="input-xxlarge col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        
        <tr>
            <td valign="middle" align="right">
                &nbsp;
            </td>
            <td colspan="3">
                <span id="lblmsg" style="color: Red">&nbsp;</span>
                <input type="hidden" name="projectID" id="projectID" value="0" />
            </td>
        </tr>
    </table>
    <div class="modal-footer">
        <button id="btnSave" type="button" class="btn btn-primary">
            保存</button>
        <button type="button" data-dismiss="modal" class="btn btn-default">
            取消</button>
    </div>
</div>
<div id="publishform" class="modal fade" data-backdrop="static" tabindex="-1" style="display: none;
    width: 400px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 class="modal-title" style="font-size: 17px; font-weight: bold" id="modaltitle">
            项目发布</h3>
    </div>
    <table border="0" cellpadding="0" cellspacing="0" style="margin-top: 10px;">
        <tr>
            <td align="right" width="100">
                发布时间:
            </td>
            <td align="left">
                <input type="text" id="txtReleaseTime" readonly="readonly" onfocus="WdatePicker({isShowClear:false, readOnly:true,skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss', minDate:'%y-%M-%d'})"
                    placeholder="发布时间" class="Wdate col-xs-10 col-sm-5" /><span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td align="right" width="100">
                投资起息日:
            </td>
            <td align="left">
                <input type="text" id="txtBeginDate" readonly="readonly" onfocus="WdatePicker({isShowClear:false, readOnly:true,skin:'ext',dateFmt:'yyyy-MM-dd', minDate:'%y-%M-%d'})"
                    placeholder="投资起息日" class="Wdate col-xs-10 col-sm-5" /><span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td align="right" width="100">
                借款计息日:
            </td>
            <td align="left">
                <input type="text" id="txtBorrowInterestDate" readonly="readonly" onfocus="WdatePicker({isShowClear:false, readOnly:true,skin:'ext',dateFmt:'yyyy-MM-dd', minDate:'%y-%M-{%d-7}'})"
                    placeholder="借款计息日" class="Wdate col-xs-10 col-sm-5" /><span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                &nbsp;
            </td>
            <td colspan="3">
                <span id="lblpubmsg" style="color: Red">&nbsp;</span>
                <input type="hidden" name="pubprojectID" id="pubprojectID" value="0" />
            </td>
        </tr>
    </table>
    <div class="modal-footer">
        <button id="btnPublish" type="button" class="btn btn-primary">
            发布</button>
        <button type="button" data-dismiss="modal" class="btn btn-default">
            取消</button>
    </div>
</div>
@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
     
    <script type="text/javascript">

        var cols = [

        { title: '项目编码', name: 'ProjectCode', width: 60, align: 'center' },
        { title: '项目名称', name: 'ProjectTitle', width: 100, align: 'center' },
        { title: '产品名称', name: 'ProductName', width: 60, align: 'center' },
        { title: '借款企业', name: 'EnterpriseName', width: 100, align: 'center', renderer: function (val, item, index) {

            return item["Enterprise"]["EnterpriseName"] + "(" + item["OriginalRate"] + "%" + ")";
        }
        },
        { title: '借款金额', name: 'CycleAmount', width: 80, align: 'center', renderer: function (val, item, index) {

            return "&yen" + Number(item["Financing"] / 10000).toFixed(2) + "万元";
        }
        },
        { title: '年化利率', name: 'OriginalRate', width: 30, align: 'center', renderer: function (val, item, index) {

            return item["Policy"]["Rate"] + "%";
        }
        },
        { title: '借款时长', name: 'Duration', width: 40, align: 'center', renderer: function (val, item, index) {
            var durationType = item["Policy"]["DurationType"] == 0 ? "天" : "月";
            return (item["CycleAmount"] * item["Policy"]["Duration"]) + durationType;
        }
        },
        { title: '担保公司', name: 'CompanyName', width: 80, align: 'center', renderer: function (val, item, index) {
            return item["GuaranteeCompany"]["CompanyName"] + "(" + item["GuaranteeRate"] + "%" + ")";
        }
        },
        { title: '状态', name: 'Status', width: 30, align: 'center', renderer: function (val, item, index) {

            return '已通过';
        }
        },
        { title: '操作', name: 'ID', width: 80, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
            var html = "";
            if (item["BorrowerUser"]["UserName"] == null) {
                html = '<button class="btn btn-info btn-mini"  onclick="grid_project_addMember(\'' + item["ID"] + '\')">平台开户</button>';
            }
            else {
                if (item["BorrowerUser"]["Account"]["UserName"] == null) {
                    html = '<button class="btn btn-info btn-mini"  onclick="grid_project_addAcount(\'' + item["ID"] + '\')">汇付开户</button>';
                }
                else {
                    html = '<button class="btn btn-info btn-mini"  onclick="grid_project_publish(\'' + item["ID"] + '\')">发布</button>';
                }
            }
            return html;
        }
        }
    ];

        var mmg;
        var ddlguaranteeCompany;
        var memberform = $("#memberform");
        var publishform = $("#publishform");
        $(function () {

            bindGuaranteeCompany();
            mmg = $('#projectgrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 250,
                cols: cols,
                url: '/ProjectPublish/GetProjectListForPublish',
                method: 'get',
                remoteSort: true,
                //items: items,
                //sortName: 'SECUCODE',
                //sortStatus: 'asc',
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.load();
            $("#btnSearch").click(function () {

                search();

            });

            $("#btnSave").click(function () {

                $("#btnSave").text("保存中...");
                $("#btnSave").attr("disabled", true);
                save();

            });
            $("#btnPublish").click(function () { 
                $("#btnPublish").text("发布中...");
                $("#btnPublish").attr("disabled", true);
                publish();

            });
        });

        function publish() {

            if ($('#txtReleaseTime').val() == "") {
                $('#txtReleaseTime').focus();
                $("#lblpubmsg").html('发布时间不能为空');
                $("#btnPublish").text("发布");
                $("#btnPublish").attr("disabled", false);
                return;
            }

            if ($('#txtBeginDate').val() == "") {
                $('#txtBeginDate').focus();
                $("#lblpubmsg").html('投资起息日不能为空');
                $("#btnPublish").text("发布");
                $("#btnPublish").attr("disabled", false);
                return;
            }

            if ($('#txtBorrowInterestDate').val() == "") {
                $('#txtBorrowInterestDate').focus();
                $("#lblpubmsg").html('借款计息日不能为空');
                $("#btnPublish").text("发布");
                $("#btnPublish").attr("disabled", false);
                return;
            }

            $.ajax({
                url: "/ProjectPublish/ProjectPublish",
                data: "projectID=" + $("#pubprojectID").val() + "&realeseTime=" + $('#txtReleaseTime').val() + "&beginDate=" + $("#txtBeginDate").val()
                    + "&borrowInterestDate=" + $('#txtBorrowInterestDate').val(),
                type: "post",
                success: function (result) {
                    $("#pubprojectID").val("0");
                    //写提交ajax操作
                    if (result == "1") {
                        publishform.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;项目发布成功</span>", '300px', function () {
                            search();
                        });
                    }
                    else {

                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;项目发布失败</span>", '300px');

                    }
                    $("#btnPublish").text("发布");
                    $("#btnPublish").attr("disabled", false);

                }
            });
        }


        function save() {
            var mobilereg = /^1[3|5|8]\d{9}$/;
            var idcardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}$/;
            if ($('#txtUserName').val() == "") {
                $('#txtUserName').focus();
                $("#lblmsg").html('用户名不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!emailreg.test($('#txtUserName').val())) {
                $('#txtUserName').focus();
                $("#lblmsg").html('用户名格式错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtRealName').val() == "") {
                $('#txtRealName').focus();
                $("#lblmsg").html('真实姓名不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAgentIDNO').val() == "") {
                $('#txtAgentIDNO').focus();
                $("#lblmsg").html('身份证号码不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!idcardreg.test($('#txtAgentIDNO').val())) {
                $('#txtAgentIDNO').focus();
                $("#lblmsg").html('身份证号码格式错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAgentHandPhone').val() == "") {
                $('#txtAgentHandPhone').focus();
                $("#lblmsg").html('手机不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if (!mobilereg.test($('#txtAgentHandPhone').val())) {
                $('#txtAgentHandPhone').focus();
                $("#lblmsg").html('手机号码输入错误');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }
            if ($('#txtAddress').val() == "") {
                $('#txtAddress').focus();
                $("#lblmsg").html('居住地址不能为空');
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                return;
            }

            $.ajax({
                url: "/ProjectPublish/AddMemberForPublish",
                data: "projectID=" + $("#projectID").val() + "&userName=" + $('#txtUserName').val() + "&realName=" + $("#txtRealName").val()
                    + "&idCardNo=" + $('#txtAgentIDNO').val() + "&handPhone=" + $("#txtAgentHandPhone").val()
                    + "&address=" + $("#txtAddress").val(),
                type: "post",
                success: function (result) {
                    $("#projectID").val("0");
                    //写提交ajax操作
                    if (result == "1") {
                        agentform.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;开户成功</span>", '300px', function () {
                            search();
                        });
                    }
                    else {
                        if (result == "-1") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;用户名已存在</span>", '300px');

                        }
                        else if (result == "-2") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;身份证号码错误</span>", '300px');

                        }
                        else {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;开户失败</span>", '300px');
                        }
                    }
                    $("#btnSave").text("保存");
                    $("#btnSave").attr("disabled", false);

                }
            });
        }

        function bindGuaranteeCompany() {
            $.ajax({
                url: "/GuarCompany/GetGuarCompanyListByKey",
                data: null,
                type: "get",
                success: function (result) {
                    result = eval("(" + result + ")");
                    ddlguaranteeCompany = $("#ddlguaranteeCompany");
                    ddlguaranteeCompany.empty(); //清空select下拉框

                    for (var i = 0; i < result.length; i++) {
                        $("<option value='" + result[i].ID + "'>" + result[i].CompanyName + "</option>").appendTo(ddlguaranteeCompany)//动态添加Option子项
                    }
                    ddlguaranteeCompany = ddlguaranteeCompany.selectpicker('show');
                }
            });

        }
        function search() {
            mmg.setParm({ key: $("#txtKey").val(), guaranteeCompanyID: ddlguaranteeCompany.val() });
            mmg.load({ page: 1 });
        }
        function grid_project_addMember(projectID) {
            $("#projectID").val(projectID);
            memberform.modal();
        }
        function grid_project_addAcount(projectID) {
            $.ajax({
                url: "/ProjectPublish/AddMemberAccount",
                data: "projectID=" + projectID,
                type: "post",
                success: function (result) {

                    //写提交ajax操作
                    if (result == "1") {
                        //agentform.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;开户成功</span>", '300px', function () {
                            search();
                        });
                    }

                    $("#btnSave").text("保存");
                    $("#btnSave").attr("disabled", false);

                }
            });
        }
        function grid_project_publish(projectID) {
            $("#pubprojectID").val(projectID);
            publishform.modal();
        }
         
    </script>
}
